#include "globals.h"

// 实际定义全局对象
PCA9557 io(0x19, &Wire);
uint8_t brightness = 255; // Default brightness level
bool touch_init_done = false; // 触摸屏初始化完成标志
// 函数实现
void WireInit(){
    Wire.setPins(1, 2);
    Wire.begin();
    delay(100);
    Wire.beginTransmission(0x19);
    uint8_t error = Wire.endTransmission();
    if (error != 0){
        logf("PCA9557 not found, error: %d\n", error);
    }else{
        logf("PCA9557 found");
    }
    Wire.begin();
    delay(100);
    logln("PCA9557 begin");
}

void lcd_cs_ctl(int level){
   io.digitalWrite(0, level);
//    delay(10);
}

void print_psram_info() {
    // 检查PSRAM是否可用
    if (psramFound()) {
        logln("[PSRAM] PSRAM is available");
        
        // 获取PSRAM大小
        uint32_t psram_size = ESP.getPsramSize();
        uint32_t free_psram = ESP.getFreePsram();
        uint32_t used_psram = psram_size - free_psram;
        
        logf("[PSRAM] Total: %d bytes (%.2f MB)", psram_size, psram_size / (1024.0 * 1024.0));
        logf("[PSRAM] Used: %d bytes (%.2f MB)", used_psram, used_psram / (1024.0 * 1024.0));
        logf("[PSRAM] Free: %d bytes (%.2f MB)", free_psram, free_psram / (1024.0 * 1024.0));
        logf("[PSRAM] Usage: %.1f%%", (used_psram * 100.0) / psram_size);
    } else {
        logln("[PSRAM] PSRAM is not available");
    }
}


// 获取详细的内存信息
void print_memory_info() {
    // 内部内存信息
    uint32_t free_internal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
    uint32_t total_internal = heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
    uint32_t largest_block_internal = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
    
    logln("=== Memory Information ===");
    logf("[INTERNAL] Total: %d bytes (%.2f MB)", total_internal, total_internal / (1024.0 * 1024.0));
    logf("[INTERNAL] Free: %d bytes (%.2f MB)", free_internal, free_internal / (1024.0 * 1024.0));
    logf("[INTERNAL] Largest block: %d bytes", largest_block_internal);
    
    // PSRAM内存信息
    if (psramFound()) {
        uint32_t free_psram = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
        uint32_t total_psram = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
        uint32_t largest_block_psram = heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
        
        logf("[PSRAM] Total: %d bytes (%.2f MB)", total_psram, total_psram / (1024.0 * 1024.0));
        logf("[PSRAM] Free: %d bytes (%.2f MB)", free_psram, free_psram / (1024.0 * 1024.0));
        logf("[PSRAM] Largest block: %d bytes", largest_block_psram);
    }
    
    // DMA内存信息
    uint32_t free_dma = heap_caps_get_free_size(MALLOC_CAP_DMA);
    uint32_t largest_block_dma = heap_caps_get_largest_free_block(MALLOC_CAP_DMA);
    logf("[DMA] Free: %d bytes (%.2f MB)", free_dma, free_dma / (1024.0 * 1024.0));
    logf("[DMA] Largest block: %d bytes (%.2f MB)", largest_block_dma, largest_block_dma / (1024.0 * 1024.0));
    
    // LVGL内存信息（如果启用监控）
    #if LV_USE_MEM_MONITOR
    logf("[LVGL] Used: %d bytes", lv_mem_get_used());
    logf("[LVGL] Max used: %d bytes", lv_mem_get_max_used());
    logf("[LVGL] Frag: %d%%", lv_mem_get_frag());
    #endif
    
    logln("========================");
}